var app = global.app;
var rootPath = app.rootPath;
var models = require(rootPath + '/models/index.js');
var mediaModel = models['media'];
var thumbModel = models['thumb'];
var md5 = require(rootPath + '/tool/md5.js').md5;
var Q = require(rootPath + '/tool/taskQueue.js');
var downFile = require(rootPath + '/tool/downFile.js');
var createThumb = require(rootPath + '/tool/createThumb.js');
var getImageInfo = require(rootPath + '/tool/getImageInfo.js');
var save2oss = require(rootPath + '/tool/save2oss.js');
var action = function (req, res) {
    //noinspection JSCheckFunctionSignatures
    var id = req.param('id'),
        width = req.param('width');
    if (!id || id.length !== 32 || !width || width < 200) {
        res.jsonp({
            'type':'error',
            'message':'arguments error'
        });
        return;
    }
    width = 200;
    var thumbId = md5(id + width);
    var q = new Q();
    var ret = {} , url;
    q.add(function (resp, next) {
        thumbModel.findById(thumbId, function (err, doc) {
            if (err) {
                res.jsonp(err);
                q = null;
                return;
            }
            if (doc == null) {
                next();
            } else {
                res.jsonp(doc);
                q = null;
            }
        });
    });
    q.add(function (resp, next) {
        //noinspection JSValidateTypes
        mediaModel.findById(id, function (err, doc) {
            if (err) {
                res.jsonp(err);
                q = null;
                return;
            }
            if (doc == null) {
                res.jsonp({
                    'type':'error',
                    'message':'id is undefined'
                });
                q = null;
            } else {
                if (doc.width < width) {
                    res.jsonp({
                        'type':'error',
                        'message':'arguments error'
                    });
                    q = null;
                    return;
                }
                url = doc.uri.replace('oss.aliyuncs.com', 'oss-internal.aliyuncs.com');
                next();
            }
        });
    });
    q.add(function (resp, next) {
        downFile(url, thumbId, function (_ret) {
            if (_ret.error) {
                res.jsonp(_ret);
                q = null;
                return;
            }
            ret.mix(_ret.data);
            next();
        });
    });
    q.add(function (resp, next) {
        createThumb(thumbId, id, width, function (_ret) {
            if (_ret.error) {
                res.jsonp(_ret);
                q = null;
                return;
            }
            ret.mix(_ret.data);
            next();
        });
    });
    q.add(function (resp, next) {
        getImageInfo(ret.fileName, function (_ret) {
            if (_ret.error) {
                res.jsonp(_ret);
                q = null;
                return;
            }
            ret.mix(_ret.data);
            next();
        });
    });
    q.add(function (resp, next) {
        save2oss(ret.fileName, width + '/' + ret.fileName, function (_ret) {
            if (_ret.error) {
                res.jsonp(_ret);
                q = null;
                return;
            }
            ret.mix(_ret.data);
            next();
        });
    });
    q.add(function (resp, next) {
        ret._id = thumbId;
        var thumb = new thumbModel();
        thumb.mix(ret);
        thumb.save(function (err, doc) {
            if (err) {
                res.jsonp(err);
                q = null;
                return;
            }
            ret = doc;
            next();
        });
    });
    q.add(function (resp, next) {
        res.jsonp(ret);
        q = null;
    });
    q.next();
};
app.get('/:id/:width', action);